package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;

/**
 * The element FRBRExpression is the metadata container of identifying properties related to the Expression level
 * according to the FRBR hierarchy.
 * 
 * <pre>
 * <xsd:element name="FRBRExpression">
 *   <xsd:complexType>
 * 		<xsd:complexContent>
 * 			<xsd:extension base="coreProperties">
 * 				<xsd:sequence>
 * 					<xsd:group ref="langProperties"/>
 * 				</xsd:sequence>
 * 			</xsd:extension>
 * 		</xsd:complexContent>
 *   </xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * <p>
 * The group exprProperties lists the properties that are characteristics of the FRBR Expression level.
 * 
 * <pre>
 * 	<xsd:group name="exprProperties">
 * 		<xsd:sequence>
 * 			<xsd:element ref="FRBRlanguage" minOccurs="1" maxOccurs="unbounded"/>
 * 			<xsd:element ref="FRBRtranslation" minOccurs="0" maxOccurs="unbounded"/>
 * 		</xsd:sequence>
 * 	</xsd:group>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class FRBRExpression extends CoreProperty {

	public static final String ELEMENT = "FRBRExpression";

	private final List<FRBRlanguage> _FRBRlanguages = new ArrayList<FRBRlanguage>(4);

	private List<FRBRtranslation> _FRBRtranslations;

	public List<FRBRlanguage> getFRBRlanguages() {
		return _FRBRlanguages;
	}

	public List<FRBRtranslation> getFRBRtranslations() {
		return _FRBRtranslations;
	}

	public void setFRBRtranslations(List<FRBRtranslation> fRBRtranslations) {
		_FRBRtranslations = fRBRtranslations;
	}

	/**
	 * @see org.akomantoso.core.data.CoreProperty#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		super.writeExternal(out);
		for (int i = 0, n = _FRBRlanguages.size(); i < n; i++) {
			_FRBRlanguages.get(i).writeExternal(out);
		}
		if (_FRBRtranslations != null) {
			for (int i = 0, n = _FRBRtranslations.size(); i < n; i++) {
				_FRBRtranslations.get(i).writeExternal(out);
			}	
		}
		
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.CoreProperty#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		
		if (FRBRlanguage.ELEMENT.equals(in.getLocalName())) {
			FRBRlanguage language;
			while (FRBRlanguage.ELEMENT.equals(in.getLocalName())) {
				_FRBRlanguages.add(language = new FRBRlanguage());
				language.readExternal(in);
				in.readNextStartElement();
			}
		}
		
		if (FRBRtranslation.ELEMENT.equals(in.getLocalName())) {
			_FRBRtranslations = new ArrayList<FRBRtranslation>(4);
			FRBRtranslation translation;
			while (FRBRtranslation.ELEMENT.equals(in.getLocalName())) {
				_FRBRtranslations.add(translation = new FRBRtranslation());
				translation.readExternal(in);
				in.readNextStartElement();
			}
		}
	}

}